//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.trf.member.controller;

import com.trf.member.base.Result;
import com.trf.member.entity.Member;
import com.trf.member.req.MemberREQ;
import com.trf.member.service.IMemberService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 会员信息表
 */

@RestController
@RequestMapping({"/member"})
public class MemberController {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    //注入
    @Autowired
    private IMemberService memberService;


    /**
     * 分页条件查询会员列表
     * @param page 页码
     * @param size 每页显示记录数
     * @param req 查询条件
     * @return
     */
    @PostMapping({"/list/search/{page}/{size}"})
    public Result search(@PathVariable("page") Long page,
                         @PathVariable("size") Long size,
                         @RequestBody(required = false) MemberREQ req) {
        this.logger.info("分页查询会员列表：page={},size={}", page, size);
        return this.memberService.search(page, size, req);
    }

    /**
     * 新增会员
     * @param member
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Member member) {
        boolean b = this.memberService.save(member);
        return b ? Result.ok() : Result.error("新增会员信息失败");
    }

    /**
     * 根据会员id 删除
     * @param id 会员id
     * @return
     */
    @DeleteMapping({"/{id}"})
    public Result delete(@PathVariable("id") int id) {
        boolean b = this.memberService.removeById(id);
        return b ? Result.ok() : Result.error("删除会员信息失败");
    }

    /**
     * 根据会员id查询会员信息
     * @param id 会员id
     * @return
     */
    @GetMapping({"/{id}"})
    public Result get(@PathVariable("id") int id) {
        Member member = (Member)this.memberService.getById(id);
        return Result.ok(member);
    }

    /**
     * 先查询会员id，判断该会员是否存在，进行回显
     * 然后再更新所需要修改的信息
     * @param id 会员id
     * @param member 会员信息
     * @return
     */
    @PutMapping({"/{id}"})
    public Result update(@PathVariable("id") int id, @RequestBody Member member) {
        return this.memberService.update(id, member);
    }
}
